package com.ai.bitcoin.weixin;

import com.ai.bitcoin.weixin.bo.WeiXinEventVo;
import org.apache.commons.lang3.StringUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;



/**
 *
 * @author xd
 *
 */

public class WeixinEventDom extends BaseDom<WeiXinEventVo>{

    private static final Logger logger = LoggerFactory.getLogger(WeixinEventDom.class);

    @Override
    public WeiXinEventVo parserXml(String xml) {

        if (StringUtils.isBlank(xml)) {
            return null;
        }

        SAXBuilder saxBuilder = new SAXBuilder();
        saxBuilder.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
        saxBuilder.setFeature("http://xml.org/sax/features/external-general-entities", false);
        saxBuilder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        Document document = null;

        try {
            document = saxBuilder.build(new StringReader(xml));
        } catch (JDOMException e) {
            logger.error("input dom string occured error", e);
        } catch (IOException e) {
            logger.error("input io occured error", e);
        }
        if(document != null) {
            Element root = document.getRootElement();
            if(root != null) {
                List<Element> childrens = root.getChildren();
                if (childrens != null && !childrens.isEmpty()) {
                    WeiXinEventVo weiXinEventVo = new WeiXinEventVo();
                    for (Element element : childrens) {
                        String elementName = element.getName();
                        switch (elementName) {
                            case "ToUserName" :
                                weiXinEventVo.setToUserName(element.getValue());
                                break;
                            case "FromUserName" :
                                weiXinEventVo.setFromUserName(element.getValue());
                                break;
                            case "CreateTime":
                                weiXinEventVo.setCreateTime(element.getValue());
                                break;
                            case "MsgType":
                                weiXinEventVo.setMsgType(element.getValue());
                                break;
                            case "Event":
                                weiXinEventVo.setEvent(element.getValue());
                                break;
                            case "EventKey":
                                weiXinEventVo.setEventKey(element.getValue());
                                break;
                            case "Ticket":
                                weiXinEventVo.setTicket(element.getValue());
                                break;
                            case "Latitude":
                                weiXinEventVo.setLatitude(element.getValue());
                                break;
                            case "Longitude":
                                weiXinEventVo.setLongitude(element.getValue());
                                break;
                            case "Precision":
                                weiXinEventVo.setPrecision(element.getValue());
                                break;
                            case "Content":
                                weiXinEventVo.setContent(element.getValue());
                                break;
                            case "MsgId":
                                weiXinEventVo.setMsgId(element.getValue());
                                break;
                            default:
                                break;
                        }
                    }
                    return weiXinEventVo;
                }
            }
        }
        return null;
    }

    @Override
    public String createXml(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Document document = new Document();
        Element rootElement = new Element("xml");
        for (Entry<String, Object> entry : map.entrySet()) {
            Element element = new Element(entry.getKey());
            element.setText(String.valueOf(entry.getValue()));
            rootElement.addContent(element);
        }
        document.addContent(rootElement);
        ByteArrayOutputStream byteRsp = new ByteArrayOutputStream();
        XMLOutputter xmlOut = new XMLOutputter();
        try {
            xmlOut.output(document, byteRsp);
        } catch (Exception e) {
            logger.error("output xml occured error", e);
        }
        return byteRsp.toString();
    }

}
